package com.example.choose;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.application.Platform;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;

public class HelloController {
    public TextField allnum;
    public TextField selectnum;
    public Label current;
    public Button act;
    public TextArea overall;
    public boolean keystate=false;

    public Button stop;
    public Button add;
    public Button clearall;
    public TextArea input;

    public int currentind;

    public int remain;

    class company{
        String index;
        String name;
        public company(){}
        public company(String index,String name){
            this.index=index;
            this.name=name;
        }
    }

    public ArrayList<company> companyArrayList;
    @FXML
    protected void onActButtonClick() throws InterruptedException {
        overall.setEditable(false);
        allnum.setEditable(false);
        selectnum.setEditable(false);
        int sum= companyArrayList.size();
        if(!keystate){
            keystate=true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(keystate){
                        currentind= (int)(Math.random()*sum+1);
                        Platform.runLater(() -> {
                            current.setText(companyArrayList.get(currentind-1).index+"");
                        });

                        System.out.println("key is "+keystate+"  value is"+currentind);
                        synchronized (currentThread()){
                            try {
                                currentThread().wait(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            currentThread().notify();
                        }
                    }
                }
            }).start();
        }
    }

     @FXML
     protected void onClearButtonClick(){
        allnum.setText(null);
        selectnum.setText(null);
        current.setText("0");
        keystate=false;
        input.setText(null);
        overall.setText("");
        companyArrayList=new ArrayList<>();
        act.setDisable(false);
        stop.setDisable(false);
        input.setEditable(true);
        add.setDisable(false);
        allnum.setEditable(true);
        selectnum.setEditable(true);
     }

     @FXML
     protected void onInputButtonClick(){
         Scanner scanner=new Scanner(input.getText());
            while(scanner.hasNext()){
                String index=scanner.next();
                String name=scanner.next();
               companyArrayList.add(new company(index,name));
               System.out.println(name);
            }
            input.setEditable(false);
            add.setDisable(true);

     }


    @FXML
    public void onStopButtonClick(){
        if(keystate){
            keystate=false;
            remain=Integer.parseInt(selectnum.getText())-1;
            selectnum.setText(remain+"");
            if(remain==0){
                act.setDisable(true);
                stop.setDisable(true);
            }
            overall.setText(overall.getText()+companyArrayList.get(currentind-1).index+"    "+companyArrayList.get(currentind-1).name+"\n");
            for(int i=0;i< companyArrayList.size();i++){
                if(companyArrayList.get(currentind - 1).index.equals(companyArrayList.get(i).index) )
                    companyArrayList.remove(i);
            }
            for(company item:companyArrayList){
                System.out.println(item.name);
            }
        }
    }

    public void initialize(){
        companyArrayList=new ArrayList<>();

    }
}